package com.chexiehui.jc.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aries.ui.view.radius.RadiusTextView;
import com.blankj.utilcode.util.PhoneUtils;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpActivity;
import com.chexiehui.jc.entity.SpecialCarInfo;
import com.chexiehui.jc.mvp.car.SpecialCarInquiryCovenant;
import com.chexiehui.jc.mvp.car.SpecialCarInquiryPresenter;
import com.chexiehui.jc.widget.LoadingLayout;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 特价车询价
 */
public class SpecialCarInquiryActivity extends BaseMvpActivity<SpecialCarInquiryPresenter> implements SpecialCarInquiryCovenant.View {
    @BindView(R.id.tv_car_name)
    TextView tvCarName;
    @BindView(R.id.et_person_name)
    EditText etPersonName;
    @BindView(R.id.et_person_phone)
    EditText etPersonPhone;
    @BindView(R.id.rtv_apply)
    RadiusTextView rtvApply;
    @BindView(R.id.rtv_shop_type)
    RadiusTextView rtvShopType;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.ll_name_panel)
    LinearLayout llNamePanel;
    @BindView(R.id.tv_shop_address)
    TextView tvShopAddress;
    @BindView(R.id.tv_shop_brand)
    TextView tvShopBrand;
    @BindView(R.id.tv_shop_distance)
    TextView tvShopDistance;
    @BindView(R.id.rtv_shop_consult)
    RadiusTextView rtvShopConsult;
    @BindView(R.id.loadingLayout)
    LoadingLayout loadingLayout;

    private SpecialCarInfo carInfo;

    @Override
    protected void beforeSetView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            carInfo = (SpecialCarInfo) bundle.getSerializable("entity");
        }
    }

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("询价")
                .setDividerVisible(false);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_special_car_inquiry;
    }

    @Override
    protected void initView(Bundle var1) {
        if (carInfo == null) {
            loadingLayout.setStatus(LoadingLayout.Empty);
            loadingLayout.setEmptyText("没有找到任何数据");
            return;
        }
        loadingLayout.setStatus(LoadingLayout.Success);
        tvCarName.setText(carInfo.getName());
        tvShopName.setText(carInfo.getShopName());
        tvShopAddress.setText(carInfo.getAdress());
    }

    @OnClick({R.id.rtv_apply, R.id.rtv_shop_consult})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rtv_apply:
                mvpPresenter.addShopInquiry();
                break;
            case R.id.rtv_shop_consult:
                callPhone();
                break;
        }
    }

    private void callPhone() {
        AndPermission.with(this)
                .permission(Permission.PHONE)
                .callback(new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        PhoneUtils.call(carInfo.getTel());
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                        if (AndPermission.hasAlwaysDeniedPermission(mContext, deniedPermissions)) {
                            AndPermission.defaultSettingDialog(mContext, Constants.REQUEST_CODE_LOACTION).show();
                        } else {
                            showToast(getStringSource(R.string.app_name) + "需要您授权电话权限");
                        }
                    }
                })
                .requestCode(Constants.REQUEST_CODE_CARAME)
                .rationale((requestCode, rationale) -> AndPermission.rationaleDialog(mContext, rationale).show()).start();
    }

    @Override
    protected SpecialCarInquiryPresenter createPresenter() {
        return new SpecialCarInquiryPresenter(this);
    }

    @Override
    public void onAddShopInquirySuccess(BaseModel<Object> successResult) {
        showToast("提价成功，销售顾问会尽快与您联系");
        onBackPressed();
    }

    @Override
    public void onAddShopInquiryFailure(BaseModel<Object> failureResult) {
        showToast(failureResult.getResultInfo());
    }

    @Override
    public String getCarId() {
        return carInfo == null ? "" : carInfo.getCarId();
    }

    @Override
    public String getShopId() {
        return carInfo == null ? "" : carInfo.getCarShopId();
    }

    @Override
    public String getPersonPhone() {
        return etPersonPhone.getText().toString().trim();
    }

    @Override
    public String getPersonName() {
        return etPersonName.getText().toString().trim();
    }

}